<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="活动ID" prop="activityId">
        <el-input
          v-model="queryParams.activityId"
          placeholder="请输入活动ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="activityTitle">
        <el-input
          v-model="queryParams.activityTitle"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="活动类型" prop="activityTypeId">
        <el-select
          v-model="queryParams.activityTypeId"
          placeholder="请选择活动类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.activity_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="开始时间" prop="activityStartTime">
        <el-date-picker
          clearable
          v-model="queryParams.activityStartTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="结束时间" prop="activityEndTime">
        <el-date-picker
          clearable
          v-model="queryParams.activityEndTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="发起人" prop="activityInitiator">
        <el-input
          v-model="queryParams.activityInitiator"
          placeholder="请输入发起人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="发起人ID" prop="activityInitiatorId">
        <el-input
          v-model="queryParams.activityInitiatorId"
          placeholder="请输入发起人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['activity:checkActivities:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['activity:checkActivities:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="checkActivitiesList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="activityId" />
      <el-table-column label="标题" align="center" prop="activityTitle" />
      <el-table-column label="活动类型" align="center" prop="activityTypeId">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.activity_type"
            :value="scope.row.activityTypeId"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="开始时间"
        align="center"
        prop="activityStartTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.activityStartTime, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="结束时间"
        align="center"
        prop="activityEndTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activityEndTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="进行状态" align="center" prop="activityProgress">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.activity_progress"
            :value="scope.row.activityProgress"
          />
        </template>
      </el-table-column>
      <el-table-column label="发起人" align="center" prop="activityInitiator" />
      <el-table-column
        label="发起时间"
        align="center"
        prop="activityInitiationTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.activityInitiationTime, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间"
        align="center"
        prop="activityUpdateTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.activityUpdateTime, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="160"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['activity:summary:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 提交活动总结OR活动评价对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动心得" prop="activitySummary">
          <editor
            v-model="form.activitySummary"
            placeholder="请输入活动心得"
            :min-height="192"
          />
        </el-form-item>
        <el-form-item
          label="评分"
          prop="activityAvgPoint"
          style="margin-bottom: 1px"
        >
          <el-rate
            v-model="form.activityAvgPoint"
            :max="5"
            show-score
            text-template="{value} 分"
          ></el-rate>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCheckActivities,
  getCheckActivities,
} from "@/api/activity/checkActivities";
import { updateActivitySummary } from "@/api/activity/activities";

import { getToken } from "@/utils/auth";

export default {
  name: "CheckActivities",
  dicts: ["activity_status", "activity_type", "activity_progress"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动审核表格数据
      checkActivitiesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 审核状态时间范围
      daterangeActivityInitiationTime: [],
      // 审核状态时间范围
      daterangeActivityUpdateTime: [],
      // 审核状态时间范围
      daterangeActivityReviewTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        activityId: null,
        activityTitle: null,
        activityTypeId: null,
        activityStartTime: null,
        activityEndTime: null,
        activityInitiator: null,
        activityInitiatorId: null,
      },
      // 表单参数
      form: {
        activitySummary: null,
        activityAvgPoint: 0.0,
      },
      // 表单校验
      rules: {
        // activityReviewComment: [
        //   { required: true, message: "审核意见不能为空", trigger: "blur" }
        // ],
        // activityReviewStatus: [
        //   { required: true, message: "审核状态不能为空", trigger: "change" }
        // ]
      },
      // 导入参数
      upload: {
        // 是否显示弹出层（导入）
        open: false,
        // 弹出层标题（导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url:
          process.env.VUE_APP_BASE_API + "/activity/checkActivities/importData",
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询活动审核列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (
        null != this.daterangeActivityInitiationTime &&
        "" != this.daterangeActivityInitiationTime
      ) {
        this.queryParams.params["beginActivityInitiationTime"] =
          this.daterangeActivityInitiationTime[0];
        this.queryParams.params["endActivityInitiationTime"] =
          this.daterangeActivityInitiationTime[1];
      }
      if (
        null != this.daterangeActivityUpdateTime &&
        "" != this.daterangeActivityUpdateTime
      ) {
        this.queryParams.params["beginActivityUpdateTime"] =
          this.daterangeActivityUpdateTime[0];
        this.queryParams.params["endActivityUpdateTime"] =
          this.daterangeActivityUpdateTime[1];
      }
      if (
        null != this.daterangeActivityReviewTime &&
        "" != this.daterangeActivityReviewTime
      ) {
        this.queryParams.params["beginActivityReviewTime"] =
          this.daterangeActivityReviewTime[0];
        this.queryParams.params["endActivityReviewTime"] =
          this.daterangeActivityReviewTime[1];
      }
      listCheckActivities(this.queryParams).then((response) => {
        this.checkActivitiesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        activityId: null,
        activityTitle: null,
        activityContent: null,
        activityTypeId: null,
        activityStartTime: null,
        activityEndTime: null,
        activityProgress: null,
        activityInitiator: null,
        activityInitiatorId: null,
        activityInitiationTime: null,
        activityUpdateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeActivityInitiationTime = [];
      this.daterangeActivityUpdateTime = [];
      this.daterangeActivityReviewTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.activityId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const activityId = row.activityId || this.ids;
      getCheckActivities(activityId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "活动评价信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      // 确保表单数据有效（如果有必要，可以在这里做验证）
      if (this.form.activitySummary && this.form.activityAvgPoint !== null) {
        // 调用updateActivities接口
        updateActivitySummary(this.form)
          .then((response) => {
            console.log("提交成功", response);
            this.$modal.msgSuccess("提交成功");
            this.getList(); // 可以在此处执行更多成功后的操作，如关闭模态框、刷新数据等
          })
          .catch((error) => {
            console.error("提交失败", error);
            this.$message.error("提交失败，请重试！");
          });
      } else {
        this.$message.warning("请填写完整后再提交！");
      }
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "activity/checkActivities/export",
        {
          ...this.queryParams,
        },
        `checkActivities_${new Date().getTime()}.xlsx`
      );
    },

    /** 活动日志列表查询 */
    handleView(row) {
      const activityId = row.activityId || 0;
      // 导航到新路由
      this.$router.push("/activity/summary-log/index/" + activityId);

      // 在这里发起请求可能不是最佳选择，因为组件可能还没有挂载
      // 通常，应该在目标路由的组件内部发起请求
      // 但如果您确实需要在这里发起请求，并且后续需要处理这些数据，可以考虑使用 Vuex 来管理状态
      getActivities(activityId)
        .then((response) => {
          // 假设您想将响应数据保存到某个全局状态或传递给新路由的组件
          // 这里只是示例，具体实现取决于您的应用架构
          console.log(response); // 打印获取到的活动数据
          // 如果需要更新全局状态或父组件的状态，请在这里做
        })
        .catch((error) => {
          console.error("获取活动信息失败:", error);
          // 处理错误情况
        });
    },
  },
};
</script>
